package com.lv.service.room.card

import android.content.Context
import android.os.Parcelable
import androidx.annotation.Keep
import androidx.lifecycle.LiveData
import androidx.room.*
import com.lv.service.bean.CardColorFontBean
import com.lv.service.room.helper.InspectionCardFontConverter
import kotlinx.parcelize.Parcelize

/**
 * 作者： ygx
 * 创建日期：2022/1/8 2:42 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * App的公共业务基础数据类
 */

//3、database
@Database(entities = [CardColorBean::class], version = 1, exportSchema = false)
abstract class CardColorDatabase : RoomDatabase() {

    abstract val cardColorDao: CardColorDao

    companion object {

        private const val USER_DB_NAME = "syy_card_color_db_1"

        @Volatile
        private var instance: CardColorDatabase? = null

        @Synchronized
        fun getInstance(context: Context): CardColorDatabase {
            return instance ?: Room.databaseBuilder(
                context,
                CardColorDatabase::class.java,
                USER_DB_NAME
            ).build().also { instance = it }
        }
    }
}

//1、entity的定义
@TypeConverters(InspectionCardFontConverter::class)
@Parcelize
@Entity(tableName = "card_color_db_1")
@Keep
data class CardColorBean(
    @PrimaryKey
    val _id: Int,//主键
    val id: String,
    val enterpage: CardColorFontBean?,
    val buypage: CardColorFontBean?,
    val listpage: CardColorFontBean?,
    val cardpage: CardColorFontBean?,
    val newcardpage: CardColorFontBean?,
) : Parcelable


//2、dao层的定义
@Dao
interface CardColorDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertFindCardColor(cardColorBean: CardColorBean)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertSuspendCardColor(cardColorBean: CardColorBean)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertSuspendCardColor(cardColorBeans: List<CardColorBean>)

    @Update(onConflict = OnConflictStrategy.REPLACE)
    fun updateFindCardColor(cardColorBean: CardColorBean)

    @Delete
    fun deleteFindCardColor(cardColorBean: CardColorBean)

    @Query("delete from card_color_db_1")
    fun deleteAllCardColor()

    @Query("delete from card_color_db_1")
    suspend fun deleteSuspendAllCardColor()

    // = , in , like
    @Query("select * from card_color_db_1 where id=:id")
    fun queryLiveFindCardColor(id: String = "0"): LiveData<CardColorBean>

    @Query("select * from card_color_db_1")
    fun queryLiveFindAllCardColor(): LiveData<List<CardColorBean>>

    @Query("select * from card_color_db_1 where id=:id")
    fun queryFindCardColor(id: String = "0"): CardColorBean?

    @Query("select * from card_color_db_1")
    fun queryFindAllCardColor(): List<CardColorBean>?

    @Query("select * from card_color_db_1 where id=:id")
    suspend fun getFindCardColor(id: String = "0"): CardColorBean?

    @Query("select * from card_color_db_1")
    suspend fun getAllCardColor(): List<CardColorBean>?
}



